﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace JU.Common
{
    public class LogHelper
    {
        //<summary>
        //初始化log
        //</summary>
        private static readonly ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 根据不同的级别记录异常
        /// </summary>
        /// <param name="msg">异常信息</param>
        /// <param name="level">异常级别</param>
        private static void Log(string msg, ErrorLevel level)
        {
            msg = CommonHelper.GetClassAndMethodName(3) + msg;
            switch (level)
            {
                case ErrorLevel.Error:
                    log.Error(msg);
                    break;
                case ErrorLevel.Info:
                    log.Info(msg);
                    break;
                case ErrorLevel.Fatal:
                    log.Fatal(msg);
                    break;
                case ErrorLevel.Debug:
                    log.Debug(msg);
                    break;
                case ErrorLevel.Warn:
                    log.Warn(msg);
                    break;
            }
        }

        //<summary>
        //记录异常
        //</summary>
        //<param name="ex">异常</param>
        public static void LogError(string msg)
        {
            Log(msg, ErrorLevel.Error);
        }

        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="msg">信息</param>
        public static void LogInfo(string msg)
        {
            Log(msg, ErrorLevel.Info);
        }

        /// <summary>
        /// 记录严重异常
        /// </summary>
        /// <param name="ex">异常</param>
        public static void LogFatal(string msg)
        {
            Log(msg, ErrorLevel.Fatal);
        }

        /// <summary>
        /// 记录bug
        /// </summary>
        /// <param name="msg">信息</param>
        public static void LogDebug(string msg)
        {
            Log(msg, ErrorLevel.Debug);
        }

        /// <summary>
        /// 记录警告
        /// </summary>
        /// <param name="msg">警告信息</param>
        public static void LogWarn(string msg)
        {
            Log(msg, ErrorLevel.Warn);
        }
    }
}
